﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLKTX1._0.DTO;
using QLKTX1._0.DB;
using System.Data.SqlClient;
using System.Data;

namespace QLKTX1._0.DAO
{
    class TangDAO
    {
        public static List<TangDTO> dsTang()
        {
            // buoc 1
            List<TangDTO> dsTang = new List<TangDTO>();//ten doi tuong list

            // buoc 2
            ConnectDB con = new ConnectDB();

            DataTable dt = con.getDataTable("SELECT * FROM Tang");//

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                // khoi tao tang
                TangDTO tang = new TangDTO();

                tang.MaTang = dt.Rows[i]["maTang"].ToString();

                tang.TenTang = dt.Rows[i]["tenTang"].ToString();

                // int x= Int32.Parse(dt.Rows[i]["maTang"].ToString());

                //double x = Double.Parse(dt.Rows[i]["maTang"].ToString());           
                // them vao List
                dsTang.Add(tang);

            }

            return dsTang;
        }

        public static bool themTang(TangDTO tang)
        {
            ConnectDB con = new ConnectDB();// dung DB

            bool rs = con.executeQuery("INSERT INTO Tang(maTang,tenTang) VALUES('" + tang.MaTang + "',N'" + tang.TenTang + "')");

            return rs;
            //return true;
        }
        public static bool xoaTang(TangDTO tang)
        {
            ConnectDB con = new ConnectDB();

            return con.executeQuery("DELETE FROM Tang WHERE maTang='" + tang.MaTang + "'");
        }

        public static bool capNhatTang(TangDTO tang)
        {
            ConnectDB con = new ConnectDB();

            return con.executeQuery("UPDATE Tang set tenTang=N'" + tang.tenTang + "' WHERE maTang='" + tang.MaTang + "'");
        }
    }
}
